package com.lwj.project1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.tabs.TabLayout;
import com.lwj.project1.myToast.MyToast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragments;
    private MyPagerAdapter myPagerAdapter;
    private SharedPreferences sharedPreferences;
    private String state;
    private MyToast myToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myToast = new MyToast(getApplicationContext());
        sharedPreferences = getSharedPreferences("project_content",MODE_PRIVATE);;
        bottomNavigationView = findViewById(R.id.navigationbar);
        viewPager = findViewById(R.id.Main_vp);
        // 初始化一个Fragment列表
        initData();

        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(myPagerAdapter);
        // 设置一个消息数量提示
        BadgeDrawable badgeDrawable = bottomNavigationView.getOrCreateBadge(R.id.Chat);
        badgeDrawable.setNumber(10);

        // 设置页面滑动与底部导航栏绑定
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch(position){
                    case 0:
                        bottomNavigationView.setSelectedItemId(R.id.Recommend);
                        break;
                    case 1:
                        checkState();
                        bottomNavigationView.setSelectedItemId(R.id.Rent);
                        break;
                    case 2:
                        checkState();
                        bottomNavigationView.setSelectedItemId(R.id.Chat);
                        break;
                    case 3:
                        checkState();
                        bottomNavigationView.setSelectedItemId(R.id.Contacts);
                        break;
                    case 4:
                        bottomNavigationView.setSelectedItemId(R.id.Person);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        // 设置底部导航栏与页面切换绑定
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()){
                case R.id.Recommend:
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.Rent:
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.Chat:
                    viewPager.setCurrentItem(2);
                    bottomNavigationView.removeBadge(R.id.Chat);
                    break;
                case R.id.Contacts:
                    viewPager.setCurrentItem(3);
                    break;
                case R.id.Person:
                    viewPager.setCurrentItem(4);
                    break;
            }
            return true;
        });
    }

    private void initData() {
        fragments = new ArrayList<>();
        RecommendFragment recommendFragment = new RecommendFragment();
        RentFragment rentFragment = new RentFragment();
        ChatFragment chatFragment = new ChatFragment();
        ContactsFragment contactsFragment = new ContactsFragment();
        PersonFragment personFragment = new PersonFragment();
        fragments.add(recommendFragment);
        fragments.add(rentFragment);
        fragments.add(chatFragment);
        fragments.add(contactsFragment);
        fragments.add(personFragment);
    }
    private void checkState(){
        if (sharedPreferences.getString("state","LOGOUT").equals("LOGOUT")){
            myToast.showToastWithImg("您还没有登录哦~请先登录~",getDrawable(R.drawable.toast_img));
            Intent intent = new Intent(MainActivity.this,LoginActivity.class);
            startActivity(intent);
        }
    }
}